﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.BaseLibrary.Tools.TypeTools;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.IRepository.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemManager
{
    public class ModuleRepository: BaseRepository, IModuleRepository
    {
        public ModuleRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {
        }
        public List<ModulePo> GetModulePosAll()
        {
            return _systemCenterDbSoaContext.ModulePo.OrderBy(c => c.order_by).ToList();
        }

        public ModulePo GetModulePoByModuleCode(string moduleCode)
        {
            return _systemCenterDbSoaContext.ModulePo.FirstOrDefault(c => c.module_code == moduleCode);
        }

        public bool ModuleAdd(ModulePo ModulePo)
        {
            _systemCenterDbSoaContext.ModulePo.Add(ModulePo);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }


        public bool ModuleUpdate(ModulePo ModulePo)
        {
            ModulePo db = _systemCenterDbSoaContext.ModulePo.FirstOrDefault(c => c.module_code == ModulePo.module_code);
            ModelCopier.CopyModel(ModulePo, db, "creator_name", "creator_date");
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 使用linq左连接查询模块视图,同时初始化它的父级名称
        /// </summary>
        /// <param name="moduleCode"></param>
        /// <returns></returns>
        public ModuleDto GetModuleDtoByCode(string moduleCode)
        {
            //select a.*,b.module_name as parent_name from [dbo].[module_db]  a
            //left join [module_db] b on a.parent_code=b.module_code
            // where a.module_code = 'system_conter'
            var view = (from dm in _systemCenterDbSoaContext.ModulePo
                        join pdm in _systemCenterDbSoaContext.ModulePo  //pdm 代表父级模块
                        on dm.parent_code equals pdm.module_code into leftJoinPdm   //把前面的join into到 leftJoinPdm
                        from leftJoinParent in leftJoinPdm.DefaultIfEmpty()   //把join查询一下, 如果是有null的, 就默认返回为null
                        where dm.module_code == moduleCode
                        select new { parent_name = leftJoinParent.module_name, mdb = dm }).FirstOrDefault();
            if (view == null) return null;
            ModuleDto mview = ModelCopier.CreateInstanceFrom<ModuleDto>(view.mdb);
        //    mview.parent_name = view.parent_name;

            return mview;


        }
        /// <summary>
        /// 获取模块视图,不使用linq来它的父级名称
        /// </summary>
        /// <param name="moduleCode"></param>
        /// <returns></returns>
        public ModuleDto GetModuleDtoByCodeSonSql(string moduleCode)
        {
            //            select * ,
            //(select top 1 module_name from[module_db] where module_code = a.parent_code    )   as parent_name
            //from[module_db] as a
            var mdb = _systemCenterDbSoaContext.ModulePo.FirstOrDefault(c => c.module_code == moduleCode);
            if (mdb == null) return null;
            ModuleDto mview = ModelCopier.CreateInstanceFrom<ModuleDto>(mdb);
            var p = _systemCenterDbSoaContext.ModulePo.FirstOrDefault(c => c.module_code == mdb.parent_code);
            mview.parent_name = p != null ? p.module_name : "";
            return mview;


        }
        /// <summary>
        /// 根据用户名获取用户下的所有模块
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<ModulePo> GetModulePosByUserName(string userName)
        {
            return (from m in _systemCenterDbSoaContext.ModulePo
                    join rmr in _systemCenterDbSoaContext.RoleModuleRelationPo on m.module_code equals rmr.module_code
                    join aurr in _systemCenterDbSoaContext.AdminUserRoleRelationPo on rmr.role_id equals aurr.role_id
                    where aurr.user_name == userName
                    select m
                    ).ToList();
        }
    }
}
